<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Model\AdminUser;
use App\Http\Controllers\Controller;
use App\Model\Post;

class PostController extends Controller
{
    public function index(){
	    $posts = Post::where('status', 0)->orderBy('created_at', 'desc')->paginate(5);
        return view('/admin/post/index', compact('posts'));
    } 

    public function status(Post $post){
    	$request = request();
    	$status = $request->input('status');
        $post->status = $status;
        try{
            $post->save();
            $result = array(
                'success' => true,
                'message' => 'Update successfully',
            );
        }    
        catch(\Exception $e){
            $result = array(
                'success' => false,
                'message' => 'Update failed',
            );
        }    

        return response()->json($result, $result['success']?200:422); 
    }
}
